package com.zking.frame.zc;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import com.zking.db.BaseDao;
import com.zking.utils.StringUtils;

/**
 * 增加会员
 * @author ZC
 */
/**
 * 增加商品界面
 * @author ZC
 *
 */
public class UpdateMember extends JDialog{
	//边框
	private Border border=BorderFactory.createMatteBorder(0,0,3,2,Color.gray);
	
	private static final long serialVersionUID = 1L;
	private JLabel lb_pinYinCode;//书名
	private JTextField tf_pinYinCode;
	private JLabel lb_bankAccount;//作者
	private JTextField tf_bankAccount;
	private JLabel lb_address;
	private JTextField tf_address;//地址
	private JLabel lb_clientName;
	private JTextField tf_clientName;//客户名
	private JLabel lb_city;
	private JTextField tf_city;//会员打折
	private JLabel lb_tel;
	private JTextField tf_tel;//初期库存
	private JLabel lb_postalCode ;
	private JTextField tf_postalCode ;//出版社
	private JLabel lb_faxed  ;
	private JTextField tf_faxed  ;//成本价
	private JLabel lb_Email;
	private JTextField tf_Email;//定价
	private JLabel lb_contactPerson;
	private JTextField tf_contactPerson;//零售价
	private JLabel lb_contactTel;
	private JTextField tf_contactTel;//零售折扣
	private JLabel lb_discount;
	private JTextField tf_discount;//批发价
	private JLabel lb_VIP;
	private JComboBox<String> cbx_VIP;//批发折扣
	
	
	private JPanel pl_flow;
	private JLabel lb_title;//标题
	private JButton bt_sure;// 修改
	private JButton bt_cancel;// 取消
	//样式
	private Color color2 = new Color(255, 255, 225);
	private Font font = new Font("宋体", Font.BOLD, 18);
	//辅助填写数据
	private List<JComponent> list_tf=new ArrayList<JComponent>();//所有输入框

	/**
	 * 1.通过插入无文字透明标签调整距理（仅限于流式布局）
	 * @param width
	 * @param height
	 * @param c 需要调整的组件
	 */
	public void insertJLabel(int width,int height,Container c) {
		JLabel lb=new JLabel();
		lb.setPreferredSize(new Dimension(width,height));
//		lb.setOpaque(true);
//		lb.setBackground(Color.red);
		c.add(lb);
	}
	/**
	 * 2.创建带文字的按钮
	 * @param text
	 * @return
	 */
	public JButton createJButton(String text) {
		JButton bt=new JButton();
		bt=new JButton(text);
        bt.setPreferredSize(new Dimension(90, 30));
		bt.setFont(new Font("楷体",Font.BOLD,15));
		bt.setBackground(new Color(0xF0F0F0));
		bt.setBorder(border);
		bt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		return bt;
	}
	public JButton createJButton(String text,int width) {
		JButton bt=new JButton();
		bt=new JButton(text);
        bt.setPreferredSize(new Dimension(width, 30));
		bt.setFont(new Font("楷体",Font.BOLD,15));
		bt.setBackground(new Color(0xF0F0F0));
		bt.setBorder(border);
		bt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		return bt;
	}
	/**
	 * 行线
	 */
	public void createLine(int width,int height,JPanel pl) {
		JLabel lb = new JLabel();// 填充板
		lb.setPreferredSize(new Dimension(width,height));
		lb.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		pl.add(lb);
	}
	public UpdateMember(JDialog frame, boolean modal,List<String> memberInfo) {
		super(frame, modal);
		Container c = getContentPane();
		Color color0 = new Color(74, 101, 132);
		c.setBackground(color0);
		c.setLayout(new FlowLayout());
		//顶部间隔
        insertJLabel(650,25,c);
		// 中部面板
		pl_flow = new JPanel(new FlowLayout(FlowLayout.LEFT));// 流布局
		pl_flow.setPreferredSize(new Dimension(650, 340));
		Color color1 = new Color(222, 231, 239);
		pl_flow.setBackground(color1);
		c.add(pl_flow);
		
        //1.标题
		//1.1间距 
		insertJLabel(230,20, pl_flow);
		
		//1.2标题
		lb_title = new JLabel("会员基本资料");
		lb_title.setFont(new Font("楷体",Font.BOLD,30));
		lb_title.setPreferredSize(new Dimension(350, 40));
		pl_flow.add(lb_title);
        
		//2.客户名
		//2.1标签
		insertJLabel(40,20, pl_flow);
		insertJLabel(20,1,pl_flow);
		lb_clientName = new JLabel("客户名称:");
		lb_clientName.setFont(font);
		lb_clientName.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_clientName);
        //2.2输入框
		tf_clientName = new JTextField();
		tf_clientName.setFont(font);
		tf_clientName.setPreferredSize(new Dimension(100, 25));
		tf_clientName.setBackground(color2);
		tf_clientName.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_clientName);
		list_tf.add(tf_clientName);

        //3.拼音码
		//3.1标签
		insertJLabel(20,1, pl_flow);
		lb_pinYinCode = new JLabel("拼音码:");
		lb_pinYinCode.setFont(font);
		lb_pinYinCode.setPreferredSize(new Dimension(70, 40));
		pl_flow.add(lb_pinYinCode);
        //3.2输入框
		tf_pinYinCode = new JTextField();
		tf_pinYinCode.setFont(font);
		tf_pinYinCode.setPreferredSize(new Dimension(275, 25));
		tf_pinYinCode.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_pinYinCode);
		list_tf.add(tf_pinYinCode);
         
//		//那条横线
//		createLine(400,3, pl_flow);

		//4.城市
		//1标签
		insertJLabel(20,1, pl_flow);
		insertJLabel(20,1,pl_flow);
		lb_city = new JLabel("城市:");
		lb_city.setFont(font);
		lb_city.setPreferredSize(new Dimension(50, 40));
		pl_flow.add(lb_city);
        //2输入框
		tf_city = new JTextField();
		tf_city.setFont(font);
		tf_city.setPreferredSize(new Dimension(140, 25));
		tf_city.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_city);
		list_tf.add(tf_city);
		
		//5.地址
		//1标签
		insertJLabel(20,1, pl_flow);
		lb_address = new JLabel("地址:");
		lb_address.setFont(font);
		lb_address.setPreferredSize(new Dimension(50, 40));
		pl_flow.add(lb_address);
        //2输入框
		tf_address = new JTextField();
		tf_address.setFont(font);
		tf_address.setPreferredSize(new Dimension(220, 25));
		tf_address.setBackground(color2);
		tf_address.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_address);
		list_tf.add(tf_address);
		
		//6.开户行
		//1标签
		insertJLabel(100,1, pl_flow);
		insertJLabel(20,1, pl_flow);
		lb_bankAccount = new JLabel("开户行:");
		lb_bankAccount.setFont(font);
		lb_bankAccount.setPreferredSize(new Dimension(70, 40));
		pl_flow.add(lb_bankAccount);
        //2输入框
		tf_bankAccount = new JTextField();
		tf_bankAccount.setFont(font);
		tf_bankAccount.setPreferredSize(new Dimension(120, 25));
		tf_bankAccount.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_bankAccount);
		list_tf.add(tf_bankAccount);
		
		//7.电话
		//1标签
		insertJLabel(20,1, pl_flow);
		lb_tel = new JLabel("电话:");
		lb_tel.setFont(font);
		lb_tel.setPreferredSize(new Dimension(50, 40));
		pl_flow.add(lb_tel);
        //2输入框
		tf_tel = new JTextField();
		tf_tel.setFont(font);
		tf_tel.setPreferredSize(new Dimension(295, 25));
		tf_tel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_tel);
		list_tf.add(tf_tel);
		
		//行线
		insertJLabel(20,1, pl_flow);
		insertJLabel(20,1, pl_flow);
		createLine(578, 3, pl_flow);
		
		//8.邮政编码
		//1标签
		insertJLabel(20,1, pl_flow);
		insertJLabel(20,1, pl_flow);
		lb_postalCode = new JLabel("邮政编码:");
		lb_postalCode.setFont(font);
		lb_postalCode.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_postalCode);
        //2输入框
		tf_postalCode = new JTextField();
		tf_postalCode.setFont(font);
		tf_postalCode.setPreferredSize(new Dimension(480, 25));
		tf_postalCode.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_postalCode);
		list_tf.add(tf_postalCode);
		
		//9.传真
		//1标签
		insertJLabel(20,1, pl_flow);
		insertJLabel(20,1, pl_flow);
		lb_faxed = new JLabel("传真:");
		lb_faxed.setFont(font);
		lb_faxed.setPreferredSize(new Dimension(50, 40));
		pl_flow.add(lb_faxed);
        //2输入框
		tf_faxed = new JTextField();
		tf_faxed.setFont(font);
		tf_faxed.setPreferredSize(new Dimension(140, 25));
		tf_faxed.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_faxed);
		list_tf.add(tf_faxed);
		
		//10.Email
		//1.标签
		insertJLabel(20,1, pl_flow);
		lb_Email= new JLabel("Email:");
		lb_Email.setFont(font);
		lb_Email.setPreferredSize(new Dimension(70, 40));
		pl_flow.add(lb_Email);
        //2输入框
		tf_Email= new JTextField();
		tf_Email.setFont(font);
		tf_Email.setPreferredSize(new Dimension(100, 25));
		tf_Email.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_Email);
		list_tf.add(tf_Email);
		
		//11.联系人
		//1标签
		insertJLabel(10,1, pl_flow);
		lb_contactPerson = new JLabel("联系人:");
		lb_contactPerson.setFont(font);
		lb_contactPerson.setPreferredSize(new Dimension(70, 40));
		pl_flow.add(lb_contactPerson);
        //2输入框
		tf_contactPerson = new JTextField();
		tf_contactPerson.setFont(font);
		tf_contactPerson.setPreferredSize(new Dimension(80, 25));
		tf_contactPerson.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_contactPerson);
		list_tf.add(tf_contactPerson);
		
		//12.联系人手机
		//1标签
		insertJLabel(20,1, pl_flow);
		insertJLabel(20,1, pl_flow);
		lb_contactTel = new JLabel("联系人手机:");
		lb_contactTel.setFont(font);
		lb_contactTel.setPreferredSize(new Dimension(105, 40));
		pl_flow.add(lb_contactTel);
        //2输入框
		tf_contactTel = new JTextField();
		tf_contactTel.setFont(font);
		tf_contactTel.setPreferredSize(new Dimension(85, 25));
		tf_contactTel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_contactTel);
		list_tf.add(tf_contactTel);
		
		//13.折扣
		//1标签
		insertJLabel(20,1, pl_flow);
		lb_discount = new JLabel("折扣:");
		lb_discount.setFont(font);
		lb_discount.setPreferredSize(new Dimension(50, 40));
		pl_flow.add(lb_discount);
        //2输入框
		tf_discount = new JTextField();
		tf_discount.setFont(font);
		tf_discount.setPreferredSize(new Dimension(120, 25));
		tf_discount.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_discount);
		list_tf.add(tf_discount);
		
		//14.VIP
		//1标签
		insertJLabel(10,1, pl_flow);
		lb_VIP = new JLabel("VIP:");
		lb_VIP.setFont(font);
		lb_VIP.setPreferredSize(new Dimension(50, 40));
		pl_flow.add(lb_VIP);
        //2输入框
		cbx_VIP = new JComboBox<String>();
		cbx_VIP.addItem("Yes");
		cbx_VIP.addItem("No");
		cbx_VIP.setSelectedItem("Yes");
		cbx_VIP.setFont(font);
		cbx_VIP.setPreferredSize(new Dimension(80, 30));
		cbx_VIP.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(cbx_VIP);
		list_tf.add(cbx_VIP);
		
		//确认，取消
		insertJLabel(600, 5, c);//调整间距
		bt_sure=createJButton("确定");
		bt_sure.addActionListener(e->{
			for(int i=0;i<list_tf.size();i++) {
				String str=null;
				if(list_tf.get(i) instanceof JTextField) {
					JTextField tf=(JTextField)list_tf.get(i);
					str=tf.getText();
				}else {
					@SuppressWarnings("unchecked")
					JComboBox<String> cbx=(JComboBox<String>)list_tf.get(i);
					str=(String) cbx.getSelectedItem();
				}
				if("".equals(str)||str.length()==0||str==null) {
					JOptionPane.showMessageDialog(UpdateMember.this,"请将数据填写完整");
					return ;
				}
				try {
					JTextField tf=(JTextField)list_tf.get(list_tf.size()-2);
					double dis=Double.parseDouble(tf.getText());//折扣
					if(dis>1||dis<=0) {
						throw new Exception("折扣范围错误");
					}
				} catch (Exception e2) {
					// TODO: handle exception
					JOptionPane.showMessageDialog(UpdateMember.this,"折扣栏请输入0-1间的数字(不包括0)");
					return ;
				}
			}
			//设置编号等（ID）
			if(memberInfo.get(0)==null) {//增加会员
				BaseDao base=new BaseDao();
				String sql="SELECT max(id) FROM client";//查找最大的ID
				long id;//编号
				try {
					id = base.getCount(sql)+1;
//					System.out.println("id="+id);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					return ;
				}
				memberInfo.set(0,id+"");
				int location=list_tf.size();
				memberInfo.set(location+1,StringUtils.dateToString(new Date(),"yyyyMMdd")+String.format("%04d",id));
				memberInfo.set(location+2,"0");
				memberInfo.set(location+3,"0");
				memberInfo.set(location+4,"0");
				memberInfo.set(location+5,StringUtils.dateToString(new Date(),"yyyy-MM-dd"));
			}
			//用新数据覆盖原数据
			for(int i=0;i<list_tf.size();i++) {
				if(list_tf.get(i) instanceof JTextField) {
					JTextField tf=(JTextField)list_tf.get(i);
					memberInfo.set(i+1,tf.getText());
				}else {
					@SuppressWarnings("unchecked")
					JComboBox<String> cbx=(JComboBox<String>)list_tf.get(i);
					memberInfo.set(i+1,(String)cbx.getSelectedItem());
				}
			}
			
			dispose();
		});
		c.add(bt_sure);
		//5.1取消
		insertJLabel(250, 20, c);//调整间距
		bt_cancel=createJButton("取消");
		bt_cancel.addActionListener(e->{
			memberInfo.clear();
			dispose();
		});
		c.add(bt_cancel);
		//设置初始值
		for(int i=2;i<list_tf.size()-2;i++) {
			if(list_tf.get(i) instanceof JTextField) {
				JTextField tf=(JTextField) list_tf.get(i);
				tf.setText("无");
			}
		}
		{
		    JTextField tf=(JTextField) list_tf.get(list_tf.size()-2);
		    tf.setText("1.0");
		}
		//装填原数据
		if(memberInfo.get(0)!=null)//如果有数据
		for(int i=0;i<list_tf.size();i++) {
			if(list_tf.get(i) instanceof JTextField) {
				JTextField tf=(JTextField) list_tf.get(i);
				tf.setText(memberInfo.get(i+1));
			}else {
				@SuppressWarnings("unchecked")
				JComboBox<String> cbx=(JComboBox<String>) list_tf.get(i);
				cbx.setSelectedItem(memberInfo.get(i+1));
			}
		}
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				memberInfo.clear();
				System.out.println("关闭");
				super.windowClosing(e);
			}
		});
		setTitle("会员管理");
		setSize(700, 465);
		setResizable(false);// 不可增加大小
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	public static void main(String[] args) {
		new UpdateMember(null,true,new ArrayList<String>());
	}
}
